/**
 * @Author: Rain
 * @Date: 2023/09/26 15:15:24
 * @LastEditTime: 2023/11/04 21:00:10
 * @Description: Do Something
 */
import { upload } from '@/utils/upload'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      file: null,
      imgPath: ''
    }
  },
  computed: {
    ...mapGetters(['imagesUploadApi'])
  },
  methods: {
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过2MB!')
      }
      this.file = file
      this.imgPath = URL.createObjectURL(file.raw)
      this.$emit('upload', this.imgPath)
    },
    handleUpload() {
      return new Promise((resolve, reject) => {
        if (this.file) {
          if (!this.file.raw) {
            resolve(this.imgPath)
            return
          }
          upload(this.imagesUploadApi, this.file.raw)
            .then((result) => {
              const {
                data: { path }
              } = result
              this.imgPath = path.replace('/home/eladmin/', '/')
              this.$emit('upload', this.imgPath)
              this.file = null
              resolve(this.imgPath)
            })
            .catch((err) => {
              console.log(err)
            })
        } else {
          resolve(this.imgPath)
        }
      })
    }
  }
}
